package utils;

import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.SessionFactory;

public class HibernateUtil {
	private static final SessionFactory sessionFactory;
	
	static {
		
		try {
			//Deprecated 4.1
//			sessionFactory = new Configuration().configure()
//					.buildSessionFactory();
			
			Configuration configuration = new Configuration();
            configuration.configure();//"hibernate.cfg.xml"
            ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().
            								applySettings(configuration.getProperties());
            sessionFactory = configuration
                            .buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
            			
		} catch (Throwable ex) {
			System.err.println("Initialize hibernate fail " + ex);
			throw new ExceptionInInitializerError(ex);
		}
				
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}
